<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\common\HttpCurl;
use app\models\Category;
use app\models\Goods;
use yii;


class GoodsLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getAllGoods($query = [], $page = 1, $limit = 10){
        $goods = Goods::find()
            ->select('g.*,c.category_name')
            ->from('{{%goods}} g')
            ->leftJoin('{{%category}} c','c.category_id=g.category_id')
            ->filterWhere($query)
            ->andWhere(['goods_status'=>1,'is_del'=>0])
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $goods->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $goods->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'goods/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function addGoods($params,$uploadresult){
        $goods = new Goods();
        $goods->goods_name = $params['goods_name'];
        $goods->category_id = $params['category_id'];
        $goods->goods_listpic = $uploadresult;
        $goods->goods_descpic = $params['goods_descpic'];
        $goods->market_price = $params['market_price'];
        $goods->real_price = $params['real_price'];
        $goods->goods_summary = $params['goods_summary'];
        $goods->goods_desc = $params['goods_desc'];
        $goods->shipping_fee = $params['shipping_fee'];
        $goods->need_idcard = $params['need_idcard'];
        $goods->list_link = $params['list_link'];
        return $goods->save();
    }

    public function getById($id)
    {
        return Goods::find()->where(['goods_id' => $id])->asArray()->one();
    }

    public function editById($id, $params, $uploadresult)
    {
        if(!$goods = Goods::findOne($id)) return false;
        $goods->goods_name = $params['goods_name'];
        if($params['category_id']!=0)
            $goods->category_id = $params['category_id'];
        $goods->goods_descpic = $params['goods_descpic'];
        $goods->market_price = $params['market_price'];
        $goods->real_price = $params['real_price'];
        $goods->goods_summary = $params['goods_summary'];
        $goods->goods_desc = $params['goods_desc'];
        $goods->shipping_fee = $params['shipping_fee'];
        $goods->need_idcard = $params['need_idcard'];
        $goods->list_link = $params['list_link'];
        if($uploadresult)
            $goods->goods_listpic = $uploadresult;
        return $goods->save();
    }

    public function saveStatus($id)
    {
        $goods = Goods::findOne($id);
        if($goods->goods_status)
            $goods->goods_status = 0;
        else
            $goods->goods_status = 1;
        return $goods->save();
    }

    public function delete($id)
    {
        $goods = Goods::findOne($id);
        $goods->is_del = 1;
        return $goods->save();
    }

}